package de.blinkt.openvpn.advancedconnection;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.DialogPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import de.blinkt.openvpn.OpenVpnPreferencesFragment;
import de.blinkt.openvpn.R;

public class Settings_AdvancedConnection extends OpenVpnPreferencesFragment implements AdvancedConnectionSettingsView {
	
	private AdvancedConnectionSettingsPresenter mPresenter;
	private ListPreference mProxyType;
	private PreferenceScreen mHTTPProxyGroup;
	private PreferenceScreen mSOCKSProxyGroup;
	private EditTextPreference mHttpProxyServerAddress;
	private EditTextPreference mHttpProxyServerPort;
	private CheckBoxPreference mHttpProxyIndefiniteRetry;
	private EditTextPreference mSocksProxyServerAddress;
	private EditTextPreference mSocksProxyServerPort;
	private CheckBoxPreference mSocksProxyIndefiniteRetry;
	private CheckBoxPreference mRestrictReconnectToNetworks;
	private NetworkListPreference mRestrictedNetworkList;

	public Settings_AdvancedConnection() {

	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		PreferenceManager.setDefaultValues(getActivity(),R.xml.vpn_advancedconnection, false);
//		addPreferencesFromResource(R.xml.vpn_advancedconnection);
		
		extractWidgets();
		
		mPresenter = new AdvancedConnectionSettingsPresenter(this, getActivity());
		mPresenter.initView();
		
		if(mProfile != null){
			loadSettings();
		}
	}
	
	
	
	private void extractWidgets() {
		mProxyType = (ListPreference) findPreference("proxyType");
		mHTTPProxyGroup = (PreferenceScreen) findPreference("httpConfigGroup");
		mSOCKSProxyGroup = (PreferenceScreen) findPreference("socksConfigGroup");
		
		mHttpProxyServerAddress = (EditTextPreference) findPreference("httpProxyServerAddress");
		mHttpProxyServerPort = (EditTextPreference) findPreference("httpProxyServerPort");
		mHttpProxyIndefiniteRetry = (CheckBoxPreference) findPreference("httpProxyIndefiniteRetry");
		
		mSocksProxyServerAddress = (EditTextPreference) findPreference("socksProxyServerAddress");
		mSocksProxyServerPort = (EditTextPreference) findPreference("socksProxyServerPort");
		mSocksProxyIndefiniteRetry = (CheckBoxPreference) findPreference("socksProxyIndefiniteRetry");
		mRestrictReconnectToNetworks = (CheckBoxPreference) findPreference("restrictReconnectToNetworks");
		
		mRestrictedNetworkList = (NetworkListPreference) findPreference("restrictReconnectToNetworksList");


	}

	@Override
	protected void loadSettings() {
		mPresenter.loadSettings(mProfile);
		
		NetworkListPreference restrictedNetworkListPreference = (NetworkListPreference) findPreference("restrictReconnectToNetworksList");
		restrictedNetworkListPreference.getPresenter().loadSettings(mProfile);
	}

	@Override
	protected void saveSettings() {
		mPresenter.saveSettings(mProfile);
		
		NetworkListPreference restrictedNetworkListPreference = (NetworkListPreference) findPreference("restrictReconnectToNetworksList");
		restrictedNetworkListPreference.getPresenter().saveSettings(mProfile);

	}
	
	@Override
	public ListPreference getProxyType() {
		return mProxyType;
	}

	@Override
	public PreferenceScreen getHTTPProxyGroup() {
		return mHTTPProxyGroup;
	}

	@Override
	public PreferenceScreen getSOCKSProxyGroup() {
		return mSOCKSProxyGroup;
	}

	@Override
	public EditTextPreference getHttpProxyServerAddress() {
		return mHttpProxyServerAddress;
	}

	@Override
	public EditTextPreference getHttpProxyServerPort() {
		return mHttpProxyServerPort;
	}

	@Override
	public CheckBoxPreference getHttpProxyIndefiniteRetry() {
		return mHttpProxyIndefiniteRetry;
	}

	@Override
	public EditTextPreference getSocksProxyServerAddress() {
		return mSocksProxyServerAddress;
	}

	@Override
	public EditTextPreference getSocksProxyServerPort() {
		return mSocksProxyServerPort;
	}

	@Override
	public CheckBoxPreference getSocksProxyIndefiniteRetry() {
		return mSocksProxyIndefiniteRetry;
	}

	@Override
	public CheckBoxPreference getRestrictReconnectToNetworks() {
		return mRestrictReconnectToNetworks;
	}
	
	@Override
	public DialogPreference getSelectRestrictedNetworks() {
		return mRestrictedNetworkList;
	}



}
